package com.tensquare.article.service;

import com.tensquare.article.dao.CommentDao;
import com.tensquare.article.pojo.Comment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import util.IdWorker;

import java.util.List;

/**
 * @ClassName CommentService
 * @Description TODO
 * @Author zhangshuai
 * @Date 2019/1/16 13:54
 * @Version 1.0
 **/
@Service
public class CommentService {

    @Autowired
    private CommentDao commentDao;

    @Autowired
    private IdWorker idWorker;

    /**
     * 增加评论
     *
     * @param comment
     */
    public void add(Comment comment) {
        comment.set_id(idWorker.nextId() + "");
        commentDao.save(comment);
    }

    /**
     * 根据文章ID查询评论
     *
     * @param articleid
     * @return
     */
    public List<Comment> findByArticleid(String articleid) {
        return commentDao.findByArticleid(articleid);
    }

    /**
     * 删除评论(先删除子评论）
     *
     * @param id
     */
    public void delete(String id) {

        List<Comment> comments = commentDao.findByParentid(id);
        commentDao.deleteAll(comments);
        commentDao.deleteById(id);
    }

}
